import Qs from "qs";
import * as CollectionRequest from "~/assets/collection/collection";
import * as CartRequest from "~/assets/cart/cart";

export const updateUserInfo = (vm) => {
  let data = {
    avatar: vm.$store.state.userInfo.avatar,
    createTime: vm.$store.state.userInfo.createTime,
    email: vm.updateForm.email,
    id: vm.$store.state.userInfo.id,
    isActive: vm.$store.state.userInfo.isActive,
    password: vm.$store.state.userInfo.password,
    phone: vm.updateForm.tel,
    updateTime: vm.$store.state.userInfo.updateTime,
    username: vm.updateForm.name
  }
  vm.$axios.post('/user/update', data)
    .then((res) => {
      vm.$message.success("修改成功")
      getUserInfoById(vm)
    })
}

export const getUserInfoById = (vm) => {
  vm.$axios.get('/s-users/' + vm.$store.state.userInfo.id)
    .then((res) => {
      vm.$store.state.userInfo = res.data
      console.log(res)
    })
}

export const changePasswordById = (vm) => {
  let data =Qs.stringify({
      userId: vm.$store.state.userInfo.id,
      password: vm.pwForm.enterPassword
    })
  vm.$axios.post('/user/changePassword',data)
    .then((res)=>{
      if (res.data.code===200){
        vm.$message.success("修改成功")
        getUserInfoById(vm)
      }else {
        vm.$message.error("修改失败")
      }

    })
}
